package core
{
	import com.adobe.serialization.json.JSON;
	import com.adobe.serialization.json.JSONParseError;
	
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeApplication;
	import flash.desktop.NativeDragManager;
	import flash.events.Event;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.html.HTMLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import org.ais.system.Ais;
	import org.aisy.display.USprite;

	public class HtmlView extends USprite
	{
		protected var _webKit:HTMLLoader;
		protected var _uMask:USprite;
		
		public function HtmlView()
		{
			init();
		}
		
		protected function init():void
		{
			_webKit = new HTMLLoader();
			addChild(_webKit);
			_webKit.addEventListener(Event.COMPLETE, __webkitHandler);
			var url:String = NativeApplication.nativeApplication.openedWindows[0].stage.loaderInfo.url.replace(/^app\:/ig, "");
			url = url.replace(/(.*)(?<=[\\\/]).*/i, "$1") + "json.html";
			url = "file://" + File.applicationDirectory.nativePath + url;
			_webKit.load(new URLRequest(url));
			
			addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, __nativeDragHandler);
			addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT, __nativeDragHandler);
			addEventListener(NativeDragEvent.NATIVE_DRAG_OVER, __nativeDragHandler);
			addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, __nativeDragHandler);
			
			Ais.IMain.stage.addEventListener(Event.RESIZE, __stageHandler);
			
			Ais.IMain.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		protected function jsonValidate():void
		{
			try {
				JSON.decode(getJsJsonValue());
			}
			catch (error:JSONParseError) {
				_webKit.window.JE.showError(error);
			}
		}
		
		protected function showMask():void
		{
			if (null !== _uMask) return;
			_uMask = new USprite();
			_uMask.graphics.beginFill(0x000000, 0.5);
			_uMask.graphics.drawRect(0, 0, Ais.IMain.stage.stageWidth, Ais.IMain.stage.stageHeight);
			_uMask.graphics.endFill();
			addChild(_uMask);
		}
		
		protected function clearMask():void
		{
			if (null === _uMask) return;
			_uMask.clear();
			_uMask = null;
		}
		
		protected function getJsJsonValue():String
		{
			return _webKit.window.JE.editUI.value;
		}
		
		protected function setJsJsonValue(value:String):void
		{
			_webKit.window.JE.editUI.value = value;
		}
		
		protected function __webkitHandler(e:Event):void
		{
			switch (e.type) {
				case Event.COMPLETE:
					_webKit.window.jsonValidate = jsonValidate;
					_webKit.window.setCursor = Main.IM.setCursor;
					break;
			}
		}
		
		protected function __nativeDragHandler(e:NativeDragEvent):void
		{
			switch (e.type) {
				case NativeDragEvent.NATIVE_DRAG_OVER:
					showMask();
					break;
				case NativeDragEvent.NATIVE_DRAG_ENTER:
					NativeDragManager.acceptDragDrop(this);
					showMask();
					break;
				case NativeDragEvent.NATIVE_DRAG_EXIT:
					clearMask();
					break;
				case NativeDragEvent.NATIVE_DRAG_DROP:
					clearMask();
					var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
					if (arr && arr.length) {
						var b:ByteArray = new ByteArray();
						var f:File = arr[0];
						var fs:FileStream = new FileStream();
						fs.open(f, FileMode.READ);
						fs.readBytes(b, 0, fs.bytesAvailable);
						fs.close();
						f.cancel();
						setJsJsonValue(b.toString());
						_webKit.window.JE.editUI.onpropertychange();
						_webKit.window.JE.format2(false);
						fs = null;
						f = null;
						b = null;
					}
					arr = null;
					break;
			}
		}
		
		protected function __stageHandler(e:Event):void
		{
			_webKit.width = Ais.IMain.stage.stageWidth;
			_webKit.height = Ais.IMain.stage.stageHeight;
		}
		
		override public function clear():void
		{
			Ais.IMain.stage.removeEventListener(Event.RESIZE, __stageHandler);
			_webKit.removeEventListener(Event.COMPLETE, __webkitHandler);
			clearMask();
			_webKit = null;
			super.clear();
		}
		
	}
}